﻿using System.Collections.Generic;
using System.Linq;

namespace CommonLib
{
    public static class WordParser
    {
        public static Dictionary<string, int> GetWords(string text)
        {
            var list = text.Split('\'', ' ', '.', ';', ':', ',', '\"', '“', '”', '(', ')', ']', '[', '$', '!', '?', '’', '0',
                '1', '2', '3', '4', '5', '6', '7', '8', '9', '/', '«', '»');

            var categoryCounts = from p in list
                                 group p by p.ToLower() into g
                                 select new { Category = g.Key, ProductCount = g.Count() };

            var dic = new Dictionary<string, int>();

            foreach (var categoryCount in categoryCounts)
            {
                var str = categoryCount.Category.Trim();

                if (str.Length == 0 || str.Length == 1)
                    continue;

                if (!dic.ContainsKey(str))
                    dic[str] = categoryCount.ProductCount;
                else
                    dic[str] += categoryCount.ProductCount;
            }

            var res = dic.ToDictionary(categoryCount => categoryCount.Key, categoryCount => categoryCount.Value);

            return res;
        }

        public static IDictionary<string, int> PrepareWords(Dictionary<string, int> words)
        {
            return words.ToDictionary(word => word.Key.ToLower(), word => word.Value);
        }
    }
}
